package cn.yanjr.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.yanjr.common.bean.BeanFactory;
import cn.yanjr.common.utils.StringUtils;
import cn.yanjr.system.dao.SysMenuDao;
import cn.yanjr.system.dao.SysUserDao;
import cn.yanjr.system.entity.SysMenu;
import cn.yanjr.system.entity.SysUser;
import cn.yanjr.system.service.SysUserService;

public class SysUserServiceImpl implements SysUserService{
	
	private SysUserDao sysUserDao = BeanFactory.getBean("sysUserDao", SysUserDao.class);
	
	private SysMenuDao sysMenuDao = BeanFactory.getBean("sysMenuDao", SysMenuDao.class);

	@Override
	public Map<String, Object> queryPage(Map<String, Object> param) {
		int pageNo = param.get("pageNo") == null ? -1 : Integer.parseInt(param.get("pageNo").toString());
		int pageSize = param.get("pageSize") == null ? 10 : Integer.parseInt(param.get("pageSize").toString());
		String company = param.get("company") == null ? "" : param.get("company").toString();
		String office = param.get("office") == null ? "" : param.get("office").toString();
		String loginname = param.get("loginname") == null ? "" : param.get("loginname").toString();
		String no = param.get("no") == null ? "" : param.get("no").toString();
		String name = param.get("name") == null ? "" : param.get("name").toString();
		String mobile = param.get("mobile") == null ? "" : param.get("mobile").toString();
		String email = param.get("email") == null ? "" : param.get("email").toString();
		String remarks = param.get("remarks") == null ? "" : param.get("remarks").toString();
		
		StringBuilder sql = new StringBuilder();
		sql
		.append("select u.*,c.name company_name,             ")
		.append("  o.name office_name,                       ")
		.append("  u1.name create_name,                      ")
		.append("  u2.name update_name                       ")
		.append("from sys_user u                             ")
		.append("left join sys_office c                      ")
		.append("	on c.id=u.company_id and c.type=1        ")
		.append("left join sys_office o on o.id=u.office_id  ")
		.append("left join sys_user u1 on u1.id=u.create_by  ")
		.append("left join sys_user u2 on u2.id=u.update_by  ")
		.append("where u.del_flag=0                          ")
		;
		if(StringUtils.isNotEmpty(company)) {
			sql.append(" and c.name like '%").append(company).append("%' ");
		}
		if(StringUtils.isNotEmpty(office)) {
			sql.append(" and o.name like '%").append(office).append("%' ");
		}
		if(StringUtils.isNotEmpty(loginname)) {
			sql.append(" and u.login_name like '%").append(loginname).append("%' ");
		}
		if(StringUtils.isNotEmpty(no)) {
			sql.append(" and u.no like '%").append(no).append("%' ");
		}
		if(StringUtils.isNotEmpty(name)) {
			sql.append(" and u.name like '%").append(name).append("%' ");
		}
		if(StringUtils.isNotEmpty(mobile)) {
			sql.append(" and u.mobile like '%").append(mobile).append("%' ");
		}
		if(StringUtils.isNotEmpty(email)) {
			sql.append(" and u.email like '%").append(email).append("%' ");
		}
		if(StringUtils.isNotEmpty(remarks)) {
			sql.append(" and u.remarks like '%").append(remarks).append("%' ");
		}
		sql.append(" order by u.create_date desc ");
		Map<String, Object> m = new HashMap<String, Object>();
		Map<String, Object> data = sysUserDao.findPage(sql.toString(), pageNo, pageSize);

		m.put("code", 0);
		m.put("msg", "查询成功！");
		m.put("totalpage", data.get("total"));
		m.put("page", pageNo);
		m.put("count", data.get("records"));
		m.put("data", data.get("rows"));
		
		
		return m;
	}

	@Override
	public SysUser get(String id) {
		return sysUserDao.findOne(id);
	}

	@Override
	public Map<String, Object> save(SysUser user) {
		sysUserDao.save(user);
		return null;
	}

	@Override
	public Map<String, Object> delete(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> getOne(String id) {
		StringBuilder sql = new StringBuilder();
		sql
		.append("select u.*,c.name company_name,             ")
		.append("  o.name office_name,                       ")
		.append("  u1.name create_name,                      ")
		.append("  u2.name update_name                       ")
		.append("from sys_user u                             ")
		.append("left join sys_office c                      ")
		.append("	on c.id=u.company_id and c.type=1        ")
		.append("left join sys_office o on o.id=u.office_id  ")
		.append("left join sys_user u1 on u1.id=u.create_by  ")
		.append("left join sys_user u2 on u2.id=u.update_by  ")
		.append("where u.del_flag=0 and u.id='").append(id).append("' ")
		;
		List<Map<String, Object>> list = sysUserDao.find(sql.toString());
		
		return list != null && list.size() > 0 ? list.get(0) : null;
	}

	@Override
	public Map<String, Object> saveRoles(Map<String, Object> param) {
		String userid = param.get("userid").toString();
		String roles = param.get("roles").toString();
		String[] roleArr = null;
		List<Object[]> list = new ArrayList<Object[]>();
		if(StringUtils.isNotEmpty(roles)) {
			roleArr = roles.split(",");
		}
		if(roleArr != null && roleArr.length > 0) {
			for(String s : roleArr) {
				Object[] o = new Object[2];
				o[0] = userid;
				o[1] = s;
				list.add(o);
			}
		}
		sysUserDao.delUserRole(userid);
		String sql = "insert into sys_user_role(user_id, role_id) values(?1, ?2)";
		sysUserDao.batchUpdate(sql, list.toArray());
		return null;
	}

	@Override
	public Map<String, Object> userMenus(Map<String, Object> param) {
		// TODO Auto-generated method stub
		String userId = param.get("userId").toString();
		int statusId = Integer.parseInt(param.get("statusId").toString());
		String pid = param.get("pid") == null ? null : param.get("pid").toString();
		List<SysMenu> list = null;
		list = sysMenuDao.getMenusByUserIdAndStatusId(userId, statusId);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("list", list);
		result.put("pid", pid);
		return result;
	}

	@Override
	public SysUser getUserByLoginName(String loginName) {
		List<SysUser> list = sysUserDao.findByLoginName(loginName);
		return list != null && list.size() > 0 ? list.get(0) : null;
	}

	@Override
	public List<SysUser> getUserByOfficeAndChild(String officeId) {
		return sysUserDao.findByOfficeAndChild("," + officeId + ",");
	}

	@Override
	public List<SysUser> getUserByOffice(String officeId) {
		return sysUserDao.findByOffice(officeId);
	}


}
